package com.ruoyi.project.erp.controller;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.erp.query.WorklogStatisticsQuery;
import com.ruoyi.project.erp.service.IWorklogService;
import com.ruoyi.project.erp.vo.DeptMemberVO;
import com.ruoyi.project.erp.vo.WorklogStatisticsSummaryVO;
import com.ruoyi.project.erp.vo.WorklogStatisticsVO;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

@RestController
@RequestMapping("/erp/worklogStatistics")
public class WorklogStatisticsController extends BaseController {

    @Autowired
    private IWorklogService worklogService;
    @Autowired
    private ISysUserService userService;



    /**
     * 查询部门工时统计列表（部门主管用）
     */
//    @PreAuthorize("@ss.hasPermi('system:worklog:statistics:dept')")
    @GetMapping("/dept")
    public AjaxResult deptList(WorklogStatisticsQuery query) {
        System.out.println("=== 原始参数 ===");
        System.out.println("userId: [" + query.getUserId() + "]");
        System.out.println("userId == null: " + (query.getUserId() == null));
        System.out.println("userId.equals(\"\"): " + (query.getUserId() != null && query.getUserId().equals("")));
        System.out.println("userId.length(): " + (query.getUserId() != null ? query.getUserId().length() : "null"));
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        // 设置当前用户的部门ID
        query.setDeptId(sysUser.getDeptId());



        System.out.println("=== 处理后参数 ===");
        System.out.println("deptId: " + query.getDeptId());
        System.out.println("startTime: " + query.getStartTime());
        System.out.println("endTime: " + query.getEndTime());
        System.out.println("userId: [" + query.getUserId() + "]");
        System.out.println("projectId: [" + query.getProjectId() + "]");
        List<WorklogStatisticsVO> list = worklogService.selectDeptWorklogStatistics(query);
        return AjaxResult.success(list);
    }
    /**
     * 获取部门工时统计汇总信息
     */
    @GetMapping("/dept/statistics")
    public AjaxResult deptStatistics(WorklogStatisticsQuery query) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        // 设置当前用户的部门ID
        query.setDeptId(sysUser.getDeptId());
        WorklogStatisticsSummaryVO summary = worklogService.selectDeptWorklogStatisticsSummary(query);
        return AjaxResult.success(summary);
    }
    /**
     * 查询个人工时统计列表
     */
    @GetMapping("/personal")
    public AjaxResult personalList(WorklogStatisticsQuery query) {
        // 设置当前用户ID
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getUser();
        query.setUserId(sysUser.getDdUserId());
        List<WorklogStatisticsVO> list =  worklogService.selectPersonalWorklogStatistics(query);
        return AjaxResult.success(list);
    }
    @GetMapping("/personal/statistics")
    public AjaxResult personalStatistics(WorklogStatisticsQuery query) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        // 设置当前用户ID
        query.setUserId(sysUser.getDdUserId());
        WorklogStatisticsSummaryVO summary = worklogService.selectPersonalWorklogStatisticsSummary(query);
        return AjaxResult.success(summary);
    }



    /**
     * 获取本部门成员列表
     */
    @GetMapping("/members")
    public AjaxResult getDeptMembers() {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        if (sysUser.getDeptId() == null) {
            return error("当前用户无归属部门");
        }

        List<DeptMemberVO> members = userService.getDeptMembersByDeptId(sysUser.getDeptId());
        return success(members);
    }

    /**
     * 获取指定部门成员列表
     */
    @GetMapping("/members/{deptId}")
    public AjaxResult getDeptMembersByDeptId(@PathVariable Long deptId) {
        List<DeptMemberVO> members = userService.getDeptMembersByDeptId(deptId);
        return success(members);
    }
}
